package com.wh.custom.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wh.basic.utils.ExcelUtils;
import com.wh.basic.vo.AjaxResult;
import com.wh.custom.dto.CustomHighSeascustomQueryDto;
import com.wh.custom.dto.CustomMycustomQueryDto;
import com.wh.custom.pojo.*;
import com.wh.custom.service.ICustomHighSeascustomService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
* 后端接口类；
*/
@RestController
@RequestMapping("/HighseasCustom")
public class CustomHighSeasCustomController {

    @Autowired
    public ICustomHighSeascustomService customHighSeascustomService;


    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody CustomHighseascustom customHighSeasCustom) {
        if ( customHighSeasCustom.getId() != null){
            customHighSeascustomService.updateById(customHighSeasCustom);
        }else{
            customHighSeascustomService.save(customHighSeasCustom);
        }
        return new AjaxResult();

    }

    /**
    * 接口：删除
    * @param id
    * @return AjaxResult 响应给前端
    */
    @DeleteMapping(value = "/{id}")
    public AjaxResult del(@PathVariable("id") Long id) {
        customHighSeascustomService.removeById(id);
        return new AjaxResult();
    }

    /**
    * 接口：批量删除
    * @param ids
    * @return AjaxResult 响应给前端
    */
    @PatchMapping
    public AjaxResult patchDel(@RequestBody List<Long> ids) {
        customHighSeascustomService.removeByIds(ids);
        return new AjaxResult();
    }

    /**
    * 接口：查询单个对象
    * @param id
    */
    @GetMapping("/{id}")
    public CustomHighseascustom findOne(@PathVariable("id") Long id) {
        return customHighSeascustomService.getById(id);
    }


    /**
    * 接口：查询所有
    * @return
    */
    @GetMapping
    public List<CustomHighseascustom> findAll() {
        return customHighSeascustomService.list();
    }


    /**
     * 接口：分页查询或高级查询

     * @return IPage<CustomMycustom> 分页对象
     */
    @PostMapping()
    public IPage<CustomHighseascustom> findByPage(@RequestBody CustomHighSeascustomQueryDto CustomHighSeascustomQueryDto) {
        return customHighSeascustomService.findByPage(CustomHighSeascustomQueryDto);
    }
    @PostMapping("/sort")
    public IPage<CustomHighseascustom> sort(@RequestBody CustomHighSeascustomQueryDto CustomHighSeascustomQueryDto) {
        return customHighSeascustomService.sort(CustomHighSeascustomQueryDto);
    }
    //数据导入
    @PostMapping("/importExcel")
    public AjaxResult importExcel(@RequestPart("file") MultipartFile file){
        //ExcelUtils.importExcel(文件对象, 标题行数, 表头行数, 生成的对象类型)
        List<CustomHighseascustom> list = ExcelUtils.importExcel(file,0,1, CustomHighseascustom.class);
        System.out.println("导入数据一共【"+list.size()+"】行");
        for (CustomHighseascustom mycustom : list) {
            //应先判断数据是否重复，如果重复则不加入到数据库中，暂时未做；
            customHighSeascustomService.save(mycustom);
        }
        return AjaxResult.me();
    }
    /*分配客户
     */
    @PutMapping("allocation")
    public AjaxResult allocation(@RequestBody Allocation allocation) {

        customHighSeascustomService.allocation(allocation);
        return new AjaxResult();
    }
    //领取客户
    @PostMapping("/receive")
    public AjaxResult recovery(@RequestBody receiveCustomer receiveCustomer) {
        customHighSeascustomService.recovery(receiveCustomer);
        return new AjaxResult();
    }
}
